import Vue from 'vue'
import VueRouter from "vue-router";
import store from "../store";

Vue.use(VueRouter)

const vueRouter = new VueRouter({
    routes: [
        {
            path: '/',
            name: 'main',
            component: () => import('@/./pages/Main'),  /*按需引入*/
            redirect: '/home', /*初始时重定向到home页面，或者把home页面的path改为空串也可以*/
            children: []
        },
        {
            path: '/login',
            name: 'login',
            meta: {
                title: '登录'
            },
            component: () => import('../pages/login')
        }
    ]
})

vueRouter.beforeEach((to, from, next) => {
    let token = store.state.login.token
    if (!token && to.name !== 'login') {
        next({name: 'login'}) /*跳转登录页面*/
    } else {
        next()
    }
})

vueRouter.afterEach((to) => {
    if (to.meta.title) {
        document.title = to.meta.title
    }
})

export default vueRouter

